package com.system.controller;


import com.sun.javafx.iio.common.ImageTools;
import com.system.Tools.Export_Excel;
import com.system.Tools.ImgTool;
import com.system.function.object.*;

import com.system.function.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/")
public class IndexController {

    @Autowired
    private CommodityService commodityService;


    //用户管理
    @RequestMapping("/")
    public ModelAndView index(HttpServletRequest request, User user) {
        ModelAndView mv = new ModelAndView("/index");
        return mv;
    }

    //用户管理
    @RequestMapping("/userAdmin")
    public ModelAndView userAdmin(HttpServletRequest request, User user) {
        ModelAndView mv = new ModelAndView("/systemIndex");
        mv.addObject("type", "userAdmin");
        return mv;
    }

    //供应商管理
    @RequestMapping("/supplierAdmin")
    public ModelAndView supplierAdmin(HttpServletRequest request, Supplier supplier) {
        ModelAndView mv = new ModelAndView("/systemIndex");
        mv.addObject("type", "supplierAdmin");
        return mv;
    }

    //仓库管理
    @RequestMapping("/warehouseAdmin")
    public ModelAndView warehouseAdmin(HttpServletRequest request, Warehousing warehousing) {
        ModelAndView mv = new ModelAndView("/systemIndex");
        mv.addObject("type", "warehouseAdmin");
        return mv;
    }

    //商品管理
    @RequestMapping("/commodityAdmin")
    public ModelAndView commodityAdmin(HttpServletRequest request, Commodity commodity) {
        ModelAndView mv = new ModelAndView("/systemIndex");
        mv.addObject("type", "commodityAdmin");
        return mv;
    }

    //商品管理
    @RequestMapping("/commodityStaticAdmin")
    public ModelAndView commodityStaticAdmin(HttpServletRequest request, Commodity commodity) {
        ModelAndView mv = new ModelAndView("/systemIndex");
        mv.addObject("type", "commodityStaticAdmin");
        return mv;
    }


    //入库管理
    @RequestMapping("/warehousingAdmin")
    public ModelAndView warehousingAdmin(HttpServletRequest request, Warehousing warehousing) {
        ModelAndView mv = new ModelAndView("/systemIndex");
        mv.addObject("type", "warehousingAdmin");
        return mv;
    }

    //出库管理
    @RequestMapping("/deliveryAdmin")
    public ModelAndView deliveryAdmin(HttpServletRequest request, Warehousing warehousing) {
        ModelAndView mv = new ModelAndView("/systemIndex");
        mv.addObject("type", "deliveryAdmin");
        return mv;
    }

    //信息管理
    @RequestMapping("/Export_Excel")
    public void Export_Excel(HttpServletRequest request,String URL, HttpServletResponse response, Commodity commodity) throws Exception {

        ArrayList<String> titleKeyList = new ArrayList<>();
        Map<String, String> columnTitleMap = new HashMap<>();
        List<Map<String, Object>> src_list = new ArrayList<>();


        //查询仓库
        List<Commodity> allCommodity_cnagku = commodityService.groupByWarehouse(commodity);
        //查询商品
        List<Commodity> allCommodity_shangp = commodityService.groupByRemarks(commodity);

        //
        titleKeyList.add("Warehouse");
        columnTitleMap.put("Warehouse", "");
        for (Commodity c_ : allCommodity_shangp) {
            titleKeyList.add(c_.getId());
            columnTitleMap.put(c_.getId(), c_.getName());
        }

        //
        for (Commodity c : allCommodity_cnagku) {
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("Warehouse", c.getWarehouse().getName());
            for (Commodity c_ : allCommodity_shangp) {
                Commodity commoditySearch = new Commodity();
                commoditySearch.setWarehouseId(c.getWarehouseId());
                commoditySearch.setName(c_.getName());
                try{
                    Commodity allCommodity = commodityService.getCommodity(commoditySearch);
                    if (null != allCommodity) {
                        dataMap.put(allCommodity.getId(), allCommodity.getStock());
                    } else {
                        dataMap.put(c_.getId(), "0");
                    }
                }catch (Exception e){
                    dataMap.put(c_.getId(), "0");
                }
            }
            src_list.add(dataMap);
        }

        Export_Excel.export_Excel(response,URL, titleKeyList, columnTitleMap, src_list);
    }

    /**
     * 作用：实现对前台传入的字节数据进行保存  保存为图片
     * @param Url
     * @param response
     */
    @RequestMapping("/imageExport")
    @ResponseBody
    public String imageExport(String Url, HttpServletResponse response) {
        try {
            String path = ImgTool.saveImg(Url);
            return path;
        } catch (Exception e) {
            return "";
        }
    }



}
